﻿<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:cs="urn:cs">


  <msxsl:script language="C#" implements-prefix="cs">
    <![CDATA[
        public int SortRating(int iAll, int iSuccess)
        {
            if(iAll == 0)
            {
                return -1;
            }

            if(iSuccess == 0)
            {
                return 0;
            }

            double dRet1 = 100 * iSuccess / iAll;
            return (int)Math.Round(dRet1);
        }
        
        
        public string CalcRating(int iAll, int iSuccess)
        {
            if(iAll == 0)
            {
                return "-";
            }

            if(iSuccess == 0)
            {
                return "0 %";
            }

            double dRet1 = 100 * iSuccess / iAll;
            double dRet2 = Math.Round(dRet1);

            return dRet2.ToString() + " %";
        }
        
        public string GetTitle(string sUri)
        {
           return sUri.Substring(sUri.LastIndexOf("/") + 1);
        }
     ]]>
  </msxsl:script>






  <xsl:template match="/">
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <style type="text/css">
          h1 {
          color: green;
          font-family: Tahoma;
          font-size:16px;
          }

          table {
          font-family: Tahoma;
          font-size:12px;
          }

          td {
          padding-left:15px;
          padding-right:15px;
          padding-top:4px;
          padding-bottom:4px;
          }

          a {
          color: black;
          }
        </style>
        <title>Ratings Summary</title>
      </head>
      <body>
        <xsl:call-template name="ReportData">
        </xsl:call-template>
      </body>
    </html>
  </xsl:template>
  <!--=============================================================================================================================================================-->
  <!-- Template to display all data                                                                                                                            -->
  <!--=============================================================================================================================================================-->
  <xsl:template name="ReportData">

    <H1>
      <xsl:text>Topic Summary: </xsl:text>
      <xsl:value-of select="count(//Rollups)" />
    </H1>

    <table>
      <tr>
        <th noWrap="" style="BACKGROUND-COLOR: gainsboro; HEIGHT: 30px; PADDING-LEFT: 10px; PADDING-RIGHT: 10px; PADDING-TOP: 10px; PADDING-BOTTOM: 10px;">
          Topic
        </th>
        <td style="BACKGROUND-COLOR: cornsilk; TEXT-ALIGN: center">
          <a>
            <xsl:attribute name="href">
              <xsl:value-of select="//LiveUrl" />
            </xsl:attribute>
            <xsl:attribute name="target">
              <xsl:text disable-output-escaping="yes">_blank</xsl:text>
            </xsl:attribute>
            <xsl:value-of select="cs:GetTitle(//LiveUrl)" />
          </a>
        </td>
      </tr>

      <tr>
        <th noWrap="" style="BACKGROUND-COLOR: gainsboro; HEIGHT: 30px; PADDING-LEFT: 10px; PADDING-RIGHT: 10px; PADDING-TOP: 10px; PADDING-TOP: 10px; PADDING-BOTTOM: 10px;">
          Rating
        </th>
        <td style="BACKGROUND-COLOR: cornsilk; TEXT-ALIGN: center">
          <xsl:value-of select="cs:CalcRating(sum(//RatingCount), sum(//HelpfulCount))" />
        </td>
      </tr>
      <tr>
        <th noWrap="" style="BACKGROUND-COLOR: gainsboro; HEIGHT: 30px; PADDING-LEFT: 10px; PADDING-RIGHT: 10px; PADDING-TOP: 10px; PADDING-BOTTOM: 10px;">
          Ratings Count
        </th>
        <td style="BACKGROUND-COLOR: cornsilk; TEXT-ALIGN: center">
          <xsl:value-of select="sum(//RatingCount)" />
        </td>
      </tr>
      <tr>
        <th noWrap="" style="BACKGROUND-COLOR: gainsboro; HEIGHT: 30px; PADDING-LEFT: 10px; PADDING-RIGHT: 10px; PADDING-TOP: 10px; PADDING-BOTTOM: 10px;">
          Helpful Count
        </th>
        <td style="BACKGROUND-COLOR: cornsilk; TEXT-ALIGN: center">
          <xsl:value-of select="sum(//HelpfulCount)" />
        </td>
      </tr>
      <tr>
        <th noWrap="" style="BACKGROUND-COLOR: gainsboro; HEIGHT: 30px; PADDING-LEFT: 10px; PADDING-RIGHT: 10px; PADDING-TOP: 10px; PADDING-BOTTOM: 10px;">
          Total Page Views
        </th>
        <td style="BACKGROUND-COLOR: cornsilk; TEXT-ALIGN: center">
          <xsl:value-of select="sum(//TotalPageViewCount)" />
        </td>
      </tr>

      <tr>
        <th noWrap="" style="BACKGROUND-COLOR: gainsboro; HEIGHT: 30px; PADDING-LEFT: 10px; PADDING-RIGHT: 10px; PADDING-TOP: 10px; PADDING-BOTTOM: 10px;">
          Distinct Visitors
        </th>
        <td style="BACKGROUND-COLOR: cornsilk; TEXT-ALIGN: center">
          <xsl:value-of select="sum(//DistinctVisitorCount)" />
        </td>
      </tr>
    </table>
  </xsl:template>
</xsl:stylesheet>
